package com.tripwego.client.ui.pageAccueil;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FormCreateTripWidget extends Composite implements IHomeView, ClickHandler {

	protected Presenter presenter;

	protected EventBus eventBus;

	/*
	 * Elements graphiques
	 */
	private VerticalPanel contentPanel = new VerticalPanel();

	private HorizontalPanel panelActions = new HorizontalPanel();

	private final Button button_add = new Button("");
	private final FlexTable flexTable = new FlexTable();
	private final Label lbl_tripName = new Label("Nom du voyage (ex : Road Trip USA 2008)");
	private final TextBox textBox_tripName = new TextBox();

	/**
	 * 
	 */
	public FormCreateTripWidget() {
		super();

		contentPanel.setStyleName("popupFormulaire");

		contentPanel.add(flexTable);

		flexTable.setWidget(0, 0, lbl_tripName);
		textBox_tripName.setVisibleLength(40);

		flexTable.setWidget(1, 0, textBox_tripName);
		flexTable.getFlexCellFormatter().setColSpan(3, 0, 1);
		flexTable.getFlexCellFormatter().setColSpan(1, 0, 1);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 1);
		flexTable.getFlexCellFormatter().setColSpan(2, 0, 1);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);
		flexTable.getFlexCellFormatter().setColSpan(1, 0, 2);
		flexTable.getFlexCellFormatter().setColSpan(2, 0, 2);
		flexTable.getFlexCellFormatter().setColSpan(3, 0, 2);

		panelActions.setSpacing(10);

		contentPanel.add(panelActions);
		panelActions.setWidth("100%");
		button_add.setText("Cr\u00E9er son voyage");

		panelActions.add(button_add);
		panelActions.setCellWidth(button_add, "1%");

		contentPanel.setWidth("350px");

		button_add.addClickHandler(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.Widget#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return contentPanel;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public void onClick(ClickEvent event) {
		Object sender = event.getSource();

		if (sender == button_add) {

			boolean erreur = false;
			removeAllError();

			if (textBox_tripName.getValue() == null || textBox_tripName.getValue().toString().isEmpty()) {
				textBox_tripName.setStyleDependentName("error", true);
				erreur = true;
			}

			if (!erreur) {
				presenter.onCreateTrip(textBox_tripName.getValue());
			}
		}
	}

	@Override
	public void init() {
		//textBox_tripName.setValue("Road Trip...");
		removeAllError();
	}

	/**
	 * suppression des erreurs
	 */
	public void removeAllError() {
		textBox_tripName.setStyleDependentName("error", false);
	}

}
